package com.burger.counter.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.burger.counter.beans.Item;
import com.burger.counter.beans.ShoppingCart;
import com.burger.counter.beans.Toppings;
import com.burger.counter.business.ItemBusiness;
import com.burger.counter.business.ToppingsBusiness;

public class Topping2Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("i am in toppings servlet");
		ToppingsBusiness burgerBI = new ToppingsBusiness();
		Toppings toppingList[] =null;
		ItemBusiness ItemBI = new ItemBusiness();
		Item item;
		HttpSession session = request.getSession(true);
		
		Integer regionId =(Integer)session.getAttribute("RegionId");
		try {
			toppingList = burgerBI.getToppingsDetails(regionId);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		int userId = (Integer)session.getAttribute("UserId");
		String topping1name = (String) request.getParameter("toppingName");
		Integer	toppingId = Integer.parseInt(request.getParameter("toppingId"));
		ShoppingCart cart=(ShoppingCart)session.getAttribute("Cart");
		if(!topping1name.equals("nochoice")){
			item = ItemBI.setItemDetails(toppingId,userId, topping1name, 0.0);
			if(cart == null){
				System.out.println("Creating new cart....................");
				cart = new ShoppingCart();
				cart.addItemInCart(item);
				session.setAttribute("Cart", cart);
			}else{
				System.out.println("Cart is already there..................");
				cart.addItemInCart(item);
				session.setAttribute("Cart", cart);
			}
		}
		session.setAttribute("toppingData", toppingList);
//		for(int i=0;i<burgerList.length;i++)
//		{
//			System.out.println("ID:" + burgerList[i].getBurgerName());
//			System.out.println("Name:" + burgerList[i].getBurgerWeight());
//			
//		}
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/topping2.jsp");
		dispatcher.forward(request, response);
	}
}
